import React,{useState,useEffect} from 'react';
import {Table,Button,Modal,notification} from 'antd'
import axios from '_axios@0.27.2@axios';
import { EditOutlined,DeleteOutlined,ExclamationCircleOutlined,FileDoneOutlined } from '@ant-design/icons';
const NewsDraft = (props) => {
  const [dataSource, setdataSource] = useState([])
  const columns = [
    {
      title: 'ID',
      dataIndex: 'id',
      render: (id) => {
        return <b>{id}</b>
      }
    },
    {
      title: '新闻标题',
      dataIndex: 'title',
      render: (title,item) => {
        return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
      }
    },
    {
      title: '作者',
      dataIndex: 'author'
    },
    {
      title: '分类',
      dataIndex: 'category',
      render: (category) => {
        return category.title
      }
    },
    {
      title: '操作',
      render: (item) => {
         return <div>
           <Button
             danger
             shape="circle"
             icon={<DeleteOutlined />}
             onClick={() => confirmMethod(item)}
           />
          <Button
             shape="circle"
             onClick={() => {
              props.history.push(`/news-manage/update/${item.id}`)
             }}
              icon={<EditOutlined />}
           />
           <Button
              type="primary"
             shape="circle"
             onClick={() => {
              handleCheck(item.id)
            }}
              icon={<FileDoneOutlined />}
            />
       </div>
      }
    }
  ]
  // 审核
  const handleCheck = (id) => {
    axios.patch(`/news/${id}`, {
      auditState: 1
    }).then(res => {
      props.history.push('/audit-manage/list')
       notification.info({
        message: `通知`,
        description:`您可以在审核列表查看`,
        placement:'bottomRight'
      });
    })
  }
    // 删除提示
  const confirmMethod = (item) => {
    Modal.confirm({
    title: '删除',
    icon: <ExclamationCircleOutlined />,
    content: '你确认删除吗？',
    okText: '确认',
    cancelText: '取消',
      onOk() {
        deleteMethod(item)
      }
    })
  }
    // 删除
  const deleteMethod = (item) => {
    setdataSource(dataSource.filter(data => data.id !== item.id))
    axios.delete(`/news/${item.id}`)
 
  }
  useEffect(() => {
    const {username} = JSON.parse(localStorage.getItem('token'))
    axios.get(`/news?author=${username}&auditState=0&_expand=category`).then(res => {
      setdataSource(res.data)
    })
  }, [])
  return (
    <div>
      <Table
        dataSource={dataSource}
        columns={columns}
        pagination={{
          pageSize: 5
        }}
        rowKey={(item)=>item.id}
      ></Table>
    </div>
  );
}

export default NewsDraft;

